﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using System.Drawing.Imaging;
using PdfiumViewer;
using DXApplicationTangche.Utils;
using System.Web;
using System.Reflection;
using DXApplicationTangche.UC.报表.service;

namespace DXApplicationTangche.UC.订单一览新.PDFViewer
{
    public partial class FormPdf转图片 : DevExpress.XtraEditors.XtraForm
    {
        public FormPdf转图片()
        {
            InitializeComponent();
            //转成高清JPG图像:PdfRenderFlags.Annotations 改成 PdfRenderFlags.CorrectFromDpi DPI值设置成600 
        }

        private void butPDF_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                //FileName：显示文件路径+名字
                this.txtPDF.Text = dialog.FileName;
            }
        }
        /// <summary>
        /// 图片的textbox后面对应的按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butTUPIAN_Click(object sender, EventArgs e)
        {
            #region 测试SafeFileName，不用关
            //OpenFileDialog dialog = new OpenFileDialog();
            //if (dialog.ShowDialog() == DialogResult.OK)
            //{
            //    //SafeFileName：只显示文件名字
            //    this.txtJPG.SelectedText = dialog.SafeFileName;

            //}
            #endregion
            System.Windows.Forms.FolderBrowserDialog folder = new System.Windows.Forms.FolderBrowserDialog();
            if (folder.ShowDialog() == DialogResult.OK)
            {
                //SelectedPath:获取文件夹绝对路径,显示在textbox里面
                this.txtJPG.Text = folder.SelectedPath;

            }
        }
        /// <summary>
        /// PDF转图片格式的button按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butPDFtoTUPIAN_Click(object sender, EventArgs e)
        {
            string strpdfPath = txtPDF.Text.ToString();
            var pdf = PdfiumViewer.PdfDocument.Load(strpdfPath);
            var pdfpage = pdf.PageCount;
            var pagesizes = pdf.PageSizes;

            for (int i = 1; i <= pdfpage; i++)
            {
                Size size = new Size();
                size.Height = (int)pagesizes[(i - 1)].Height;
                size.Width = (int)pagesizes[(i - 1)].Width;
                //可以把".jpg"写成其他形式
                RenderPage(strpdfPath, i, size, txtJPG.Text.ToString() + "\\" + txtName.Text.ToString() + @".jpg");
            }

            MessageBox.Show("转换成功！");
        }
        public void RenderPage(string pdfPath, int pageNumber, System.Drawing.Size size, string outputPath, int dpi = 600)
        {
            using (var document = PdfiumViewer.PdfDocument.Load(pdfPath))
            using (var stream = new FileStream(outputPath, FileMode.Create))
            using (var image = GetPageImage(pageNumber, size, document, dpi))
            {
                image.Save(stream, ImageFormat.Jpeg);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageNumber">pdf文件张数</param>
        /// <param name="size">pdf文件尺寸</param>
        /// <param name="document">pdf文件位置</param>
        /// <param name="dpi"></param>
        /// <returns></returns>
        private static System.Drawing.Image GetPageImage(int pageNumber, Size size, PdfiumViewer.PdfDocument document, int dpi)
        {
            return document.Render(pageNumber - 1, size.Width, size.Height, dpi, dpi, PdfRenderFlags.CorrectFromDpi);
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                //FileName：显示文件路径+名字
                FileUtil.ConvertDocToDocx(dialog.FileName);

            }
        }
    }
   
}